<?php
	class xiaoquControl extends commonControl{
		/**
		 * 小区显示
		 */
		public function index(){
			$xiaoqu = V('xiaoqu');
			$xiaoqu->view = array(
					'citys'=>array(
							'type'=>'inner',
							'field'=>'cname,cid',
							'on'=>"citys.cid = xiaoqu.cid",
						),
				);
			
			$xiaoqu_row = $xiaoqu->field("xiaoqu.cid,qid,qname,xiaoqu.status,xiaoqu.sort,cname")->order("sort desc")->all();
			$this->assign('xiaoqu_row',$xiaoqu_row);
			$this->display();
		}
		/**
		 * 添加小区视图
		 */
		public function add_show(){
			$this->xqsheshi(); //获得小区设施 选项
			$this->get_citys();
			$this->display();
		}
		/**
		 * 获取城市信息 并且分配到 增加小区 和 修改小区模板 
		 */
		public function get_citys(){
			$db = M("citys");
			$citys = $db->where("status = 1")->all();
			$this->assign("citys",$citys);
		}
		/**
		 * 执行增加小区
		 */
		public function add_exe(){
			$xiaoqu = M('xiaoqu');
			$_POST['sheshi'] = isset($_POST['sheshi']) && !empty($_POST['sheshi'])?$_POST['sheshi']:array();
			$_POST['sheshi'] = $this->Arr2str($_POST['sheshi']);
			$xiaoqu->validate = array(
					array('qname','nonull','小区名称不能为空',2),
					array('info','nonull','小区介绍不能为空',2),
					array('zhoubian','nonull','小区周边不能为空',2),
				);
			if(!$xiaoqu->validate()){
				$this->error($xiaoqu->error);
			}else{
				if($xiaoqu->add()){
					$this->success('添加成功','index');
				}else{
					$this->error('添加失败');
				}
			}
		}
		/**
		 * 改变小区的显示
		 */
		public function status(){
			$db = M('xiaoqu');
			$_GET['status'] = $_GET['set'];
			unset($_GET['set']);
			if($db->update($_GET)){
				$this->success('修改成功','index',1);
			}else{
				$this->error('修改失败','index',1);
			}
		}
		/**
		 * 批量修改小区属性
		 */
		public function piliang(){
			$db = M('xiaoqu');
			foreach($_POST as $k=>$v){
				$db->update($v);
			}
			$this->success('修改成功!','index');
		}
		/**
		 * 小区修改
		 */
		public function modify_show(){
			if(isset($_GET['qid']) && !empty($_GET['qid'])){
				$db = M('xiaoqu');
				$xiaoqu = $db->find($_GET['qid']);
				$this->xqsheshi();
				$xiaoqu['sheshi'] = explode(',', $xiaoqu['sheshi']);
				$this->assign("xiaoqu",$xiaoqu);
				$this->display();
			}else{
				$this->error("参数错误");
			}
		}
		/**
		 * 执行小区修改
		 */
		public function modify_exe(){
			$_POST['sheshi'] = isset($_POST['sheshi']) && !empty($_POST['sheshi'])?$_POST['sheshi']:array();
			$_POST['sheshi'] = $this->Arr2str($_POST['sheshi']);
			$xiaoqu = M('xiaoqu');
			$xiaoqu->validate = array(
					array('qname','nonull','小区名称不能为空',2),
					array('info','nonull','小区介绍不能为空',2),
					array('zhoubian','nonull','小区周边不能为空',2),
			);
			if(!$xiaoqu->validate()){
				$this->error($xiaoqu->error);
			}else{
				if($xiaoqu->update()){
					$this->success('修改成功','index');
				}else{
					$this->error('修改失败');
				}
			}
		}
		/**
		 * 执行删除小区
		 */
		public function del(){
			$db= M('house');
			$qid = $_GET['qid']; //小区ID
			if($db->where(" qid = '".$qid."'")->find()){
				$this->error('该小区下面还有房屋，不能删除');
			}else{
				if($db->table('xiaoqu')->del($qid)){
					$this->success('删除成功');
				}else{
					$this->error('删除失败');
				}
			}
		}
		/**
		 * 将数组转化成为字符串
		 * @param Array $arr
		 * @return string
		 */
		public function Arr2str($arr){
			$str = '';
			$str = implode(',', $arr);
			return $str;
		}
		/**
		 * 向小区添加和修改视图里面 发送 小区配置
		 */
		public function xqsheshi($type=0){
			$sheshi = M('xqsheshi');
			$sheshi_row = $sheshi->all();
			$this->assign('sheshi_row',$sheshi_row);
		}
	}
?>